package com.epoint.utils.template.query;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.epoint.utils.JDBCUtil;
import com.epoint.utils.SQLUtil;

public class MySqlQueryAll<T> extends QueryAllTemplate<T>
{
    private Connection con = null;
    private ResultSet rs = null;
    private PreparedStatement ps = null;
    private T obj = null;
    private List<T> reslut = new ArrayList<>();

    @Override
    void getConnection() {
        con = JDBCUtil.getConnection();
    }

    @Override
    List<T> queryAllData(String sql, Class<T> clazz) {
        try {
            ps = con.prepareStatement(sql);
            System.out.println("queryData->" + ps);
            rs = ps.executeQuery();
            // while(rs.next()) 一定不能丢，否则报错!!!!!!!
            while(rs.next()) {
                obj = SQLUtil.queryObjectInfo(clazz, rs); 
                reslut.add(obj);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("根据id查询对象信息模板错误！");
        }
        return reslut;
    }

    @Override
    void closeConnection() {
        try {
            JDBCUtil.closeConnection(rs, ps, con);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("查询所有对象信息模板连接关闭失败！");
        }
    }

}
